Here are some examples of using creation methods to create mutable and immutable NSString, NSArray, and NSDictionary objects:
// Create a mutable stringThe following examples show how you can create and work with NSCalendarDates, which are always immutable:
string = [NSMutableString stringWithFormat:@"The string is %@", aString];
// Create an immutable string
string = [NSString stringWithFormat:@"The string is %@", aString];
// Create a mutable array
array = [NSMutableArray array];
anotherArray = [NSMutableArray arrayWithObjects:@"Marsha", @"Greg",
@"Cindy", nil];
// Create an immutable array
array = [NSArray arrayWithObjects:@"Bobby", @"Jan", @"Peter", nil];
// Create a mutable dictionary
dictionary = [NSMutableDictionary dictionary];
// Create an immutable dictionary
id stooges = [NSDictionary
dictionaryWithObjects:@("Mo", "Larry", "Curley")
forKeys:@("Stooge1", "Stooge2", "Stooge3")];
// Using the creation method date, create an NSCalendarDate instanceFor a detailed discussion of these classes and a more complete listing of methods, see the chapter "WebScript Programmer's Quick Reference to Foundation Classes".
// 'now' that contains the current date and time
now = [NSCalendarDate date];
// Return a string representation of 'now' using a format string
dateString = [now descriptionWithCalendarFormat:@"%B %d, %Y"];
// Using the creation method dateWithString:, create an NSCalendarDate
// instance 'newDate' from 'dateString'
newDate = [NSCalendarDate dateWithString:dateString
calendarFormat:@"%B %d, %Y"];
// Return a new date in which newDate's day field is decremented
date = [newDate addYear:0 month:0 day:-1 hour:0 minute:0 second:0];
Table of Contents
Next Section